package com.banmao.algorithm.No501To700.No543;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Answer {

    public static void main(String[] args) {

        String str = "4,-7,-3,null,null,-9,-3,9,-7,-4,null,6,null,-6,-6,null,null,0,6,5,null,9,null,null,-1,-4,null,null,null,-2";
        String[] split = str.split(",");

        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add("null".equalsIgnoreCase(s.trim()) ? null : Integer.parseInt(s.trim()));
        }

        TreeNode treeNode = cerate_LevelTree(list);
        //System.out.println(treeNode);

        Solution solution = new Solution();
        int depth = solution.diameterOfBinaryTree(treeNode);
        System.out.println(depth);

    }

    public static TreeNode cerate_LevelTree(List<Integer> list) {
        LinkedList<TreeNode> q = new LinkedList<TreeNode>();//还是建个队列
        TreeNode p;
        //Scanner scanner = new Scanner(System.in);
        //ch = scanner.nextLine();//从控制台取出输入第一个结点

        if(list.size() == 0) {
            return null;
        }
        Integer ch = list.get(0);

        TreeNode node = null;

        if (ch != null) {//若不为空则让他入队
            node = new TreeNode();//生成一个结点
            node.val = ch;//把输入的数据存进去
            q.add(node);
        }

        int index = 1;
        while (!q.isEmpty() && index < list.size()) {
            ch = list.get(index);//再从控制台取元素
            p = q.pollFirst();//每次都取出对头结点
            if (ch == null) {//若不为空则证明有左孩子
                p.left = null;
            } else {
                p.left = new TreeNode();//创建左孩子并入队
                p.left.val = ch;
                q.add(p.left);
            }
            index++;

            if(index >= list.size()) {
                break;
            }
            ch = list.get(index);//再取出一个元素
            if (ch == null) {//若不为空则证明有右孩子
                p.right = null;
            } else {
                p.right = new TreeNode();//创建右孩子并入队
                p.right.val = ch;
                q.add(p.right);
            }
            index++;
        }
        return node;
    }

}
